﻿using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Collections.Generic;
using System.IO;

namespace Winnow.SharepointDesigner.Layouts.Winnow.SharepointDesigner
{
    public partial class LogViewer : LayoutsPageBase
    {
        string defaultLOGSPath = @"C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\LOGS";
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
               
                grdLog.DataSource = GetContent(defaultLOGSPath + "\\" + Request["FileName"]);
                grdLog.AutoGenerateColumns = false;
                grdLog.Columns[0].HeaderText = Request["FileName"];
                grdLog.DataBind();
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }
        public List<Content> GetContent(string file)
        {
            FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

            try
            {
                List<Content> contents = new List<Content>();
                Content content = null;

                using (StreamReader sr = new StreamReader(fs))
                {
                    while (!sr.EndOfStream)
                    {
                        string str = sr.ReadLine();
                        if (str.Length > 0)
                        {
                            string[] line = str.Split('\t');
                            DateTime timestamp;
                            if (DateTime.TryParse(line[0].Trim(), out timestamp))
                            {
                                content = new Content();
                                content.Timestamp = timestamp;
                                content.Process = line[1].Trim();
                                content.TID = line[2].Trim();
                                content.Area = line[3].Trim();
                                content.Category = line[4].Trim();
                                content.EventID = line[5].Trim();
                                content.Level = line[6].Trim();
                                content.Message = line[7].Trim();
                                content.Guid = (string.IsNullOrEmpty(line[8].Trim()) ? Guid.Empty : new Guid(line[8].Trim()));
                                contents.Add(content);
                            }
                        }
                    }
                };
                return contents;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                fs.Close();
            }
        }

    }
    public class Content
    {
        public DateTime Timestamp { get; set; }
        public string Process { get; set; }
        public string TID { get; set; }
        public string Area { get; set; }
        public string Category { get; set; }
        public string EventID { get; set; }
        public string Level { get; set; }
        public string Message { get; set; }
        public Guid Guid { get; set; }
    }

}
